package com.ruoyi.report_jl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_jl.domain.ReportJlXjlyszbjlbBaDetail;
import com.ruoyi.report_jl.domain.vo.ReportJlXjlyszbjlbBaDetailVo;
import com.ruoyi.report_jl.mapper.ReportJlXjlyszbjlbBaDetailMapper;
import com.ruoyi.report_tg.domain.TdczbhslBaDetail;
import com.ruoyi.report_tg.domain.TdczbhslBsInfo;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jl.domain.bo.ReportJlXjlyszbjlbBsInfoBo;
import com.ruoyi.report_jl.domain.vo.ReportJlXjlyszbjlbBsInfoVo;
import com.ruoyi.report_jl.domain.ReportJlXjlyszbjlbBsInfo;
import com.ruoyi.report_jl.mapper.ReportJlXjlyszbjlbBsInfoMapper;
import com.ruoyi.report_jl.service.IReportJlXjlyszbjlbBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 细集料压碎指标记录Service业务层处理
 *
 * @author tll
 * @date 2024-07-31
 */
@RequiredArgsConstructor
@Service
public class ReportJlXjlyszbjlbBsInfoServiceImpl implements IReportJlXjlyszbjlbBsInfoService {

    private final ReportJlXjlyszbjlbBsInfoMapper baseMapper;
    private final ReportJlXjlyszbjlbBaDetailMapper reportJlXjlyszbjlbBaDetailMapper;

    /* 查询细集料压碎指标记录
     */
    @Override
    @FIllBasicInfo
    public ReportJlXjlyszbjlbBsInfoVo queryById(Long excelId){
        // 主表
        ReportJlXjlyszbjlbBsInfoVo vo = baseMapper.selectVoById(excelId);
        // 子表
        LambdaQueryWrapper<ReportJlXjlyszbjlbBaDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(excelId != null, ReportJlXjlyszbjlbBaDetail::getExcelId, excelId);
        lqw.orderByAsc(ReportJlXjlyszbjlbBaDetail::getCreateTime);
        List<ReportJlXjlyszbjlbBaDetailVo> detailList = reportJlXjlyszbjlbBaDetailMapper.selectVoList(lqw);
        vo.setDetailList(detailList);
        return vo;
    }

    /**
     * 查询细集料压碎指标记录列表
     */
    @Override
    public TableDataInfo<ReportJlXjlyszbjlbBsInfoVo> queryPageList(ReportJlXjlyszbjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportJlXjlyszbjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportJlXjlyszbjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询细集料压碎指标记录列表
     */
    @Override
    public List<ReportJlXjlyszbjlbBsInfoVo> queryList(ReportJlXjlyszbjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportJlXjlyszbjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportJlXjlyszbjlbBsInfo> buildQueryWrapper(ReportJlXjlyszbjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportJlXjlyszbjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportJlXjlyszbjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportJlXjlyszbjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportJlXjlyszbjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportJlXjlyszbjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportJlXjlyszbjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportJlXjlyszbjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportJlXjlyszbjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportJlXjlyszbjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportJlXjlyszbjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, ReportJlXjlyszbjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportJlXjlyszbjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportJlXjlyszbjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportJlXjlyszbjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportJlXjlyszbjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportJlXjlyszbjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportJlXjlyszbjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportJlXjlyszbjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobmc()), ReportJlXjlyszbjlbBsInfo::getBaobmc, bo.getBaobmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobbh()), ReportJlXjlyszbjlbBsInfo::getBaobbh, bo.getBaobbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportJlXjlyszbjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancr()), ReportJlXjlyszbjlbBsInfo::getJiancr, bo.getJiancr());
        lqw.eq(StringUtils.isNotBlank(bo.getJilr()), ReportJlXjlyszbjlbBsInfo::getJilr, bo.getJilr());
        lqw.eq(StringUtils.isNotBlank(bo.getFuhr()), ReportJlXjlyszbjlbBsInfo::getFuhr, bo.getFuhr());
        lqw.eq(bo.getRiq() != null, ReportJlXjlyszbjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportJlXjlyszbjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportJlXjlyszbjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportJlXjlyszbjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        return lqw;
    }

    /**
     * 新增细集料压碎指标记录
     */
    @Override
    public Boolean insertByBo(ReportJlXjlyszbjlbBsInfoBo bo) {
        ReportJlXjlyszbjlbBsInfo add = BeanUtil.toBean(bo, ReportJlXjlyszbjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改细集料压碎指标记录
     */
    @Override
    public Boolean updateByBo(ReportJlXjlyszbjlbBsInfoBo bo) {
        ReportJlXjlyszbjlbBsInfo update = BeanUtil.toBean(bo, ReportJlXjlyszbjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;

        if (flag) {
            reportJlXjlyszbjlbBaDetailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), ReportJlXjlyszbjlbBaDetail.class));
        }
        return flag;
//        boolean upd = baseMapper.updateById(update) > 0;
//        // 子表
//        boolean upd2 = false;
//        boolean insert = false;
//        if (ObjectUtils.isNotEmpty(bo.getDetailList())) {
//            // 根据excelId查询明细
//            QueryWrapper<ReportJlXjlyszbjlbBaDetail> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("excel_id", bo.getExcelId());
//            List<ReportJlXjlyszbjlbBaDetail> list = reportJlXjlyszbjlbBaDetailMapper.selectList(queryWrapper);
//            for (ReportJlXjlyszbjlbBaDetail det : bo.getDetailList()) {
//                if (ObjectUtils.isEmpty(list)) {
//                    // 新增
//                    insert = reportJlXjlyszbjlbBaDetailMapper.insert(det) > 0;
//                } else if (ObjectUtils.isNotEmpty(det.getDetailId())) {
//                    // 修改
//                    upd2 = reportJlXjlyszbjlbBaDetailMapper.updateById(det) > 0;
//                } else {
//                    throw new ServiceException("更新主键不能为空, 请刷新后重试!");
//                }
//            }
//        }
//        return upd && upd2 && insert;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportJlXjlyszbjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除细集料压碎指标记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
